import io
import re
import random
import time

import requests
from PIL import Image
from logzero import logger

import weibo_login
from config import WEIBO_UPDATE_NUM
from mysql_conf import mysel_conf
from utils import get_mongo_collection, re_html_tag

re_all_text = re.compile('<a href="/status/(.*)">全文</a>')
db_cookie = get_mongo_collection('weibo_cookie')
feed = get_mongo_collection('feed')
update_used = get_mongo_collection('update_used')


# cookie 从库取
def get_cookie_from_mongo():
    cookie_conf = db_cookie.find_one()
    if not cookie_conf:
        cookie, cookie_srt = weibo_login.login('18101038354', 'qingdian123')
        db_cookie.insert({'cookie': 'SUB=' + cookie["SUB"] + ';'})
        cookie_conf = db_cookie.find_one()
    return cookie_conf


# cookie 登录得到cookie
def get_cooke_from_login(id):
    cookie, cookie_srt = weibo_login.login("18101038354", 'qingdian123')
    cookie_new = 'SUB=' + cookie["SUB"] + ';'
    db_cookie.update({"_id": id}, {"$set": {"cookie": cookie_new}})
    return cookie_new


"""
    得到指定页数的内容
"""


def get_info(num=3, next_cursor='', return_info=[]):
    get_cookie_info = get_cookie_from_mongo()
    header = {'Cookie': get_cookie_info['cookie']}
    info = requests.get('https://m.weibo.cn/msg/index?format=cards', headers=header).json()
    if 'ok' in info:
        # cookie存库
        Cookie = get_cooke_from_login(get_cookie_info['_id'])
        header = {'Cookie': Cookie,
                  "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
                  }
    if next_cursor:
        update_info = requests.get(f'https://m.weibo.cn/feed/friends?version=v4&next_cursor={ next_cursor }&page=1',
                                   headers=header).json()
    else:
        update_info = requests.get('https://m.weibo.cn/feed/friends?version=v4', headers=header).json()
    for i in update_info[0]['card_group']:
        if 'mod_type' in i:
            continue
        else:
            return_info.append(i)
    if num == 0:
        return return_info
    else:
        num = num - 1
        return get_info(num, str(update_info[0]['next_cursor']), return_info)


"""
    得到数据库内微博的爬虫源
"""


def weibo_cos():
    cos_user = feed.find({'cos': 'cos'})
    return cos_user


'''
    得到指定条数的数据  返回  用户+内容的字典
'''


def get_update_config_num_for_source():
    source = get_update_theme_and_source()
    source_update_message = {}
    for source_name, theme_id in source.items():
        source_update_message[source_name] = []

    getinfo = get_info()
    getinfo.reverse()
    getinfo_copy = getinfo
    for i in getinfo_copy:
        is_pic = i['mblog'].get('pics')
        is_video = i['mblog'].get('page_info', {}).get('media_info', {}).get('stream_url')
        is_live = i['mblog'].get('page_info', {}).get('media_info', {}).get('goto') == 1

        if i['mblog']['user']['screen_name'] not in source:
            # print(i['mblog']['user']['screen_name'])
            logger.info(f'不在自动更新的列表')
            # getinfo_copy.remove(i)
            continue
        elif i['mblog'].get('retweeted_status'):
            logger.info(f'是转发微博， 跳过')
            # getinfo_copy.remove(i)
            continue

        elif not is_pic and not is_video:
            logger.info(f'无图片也无视频，跳过')
            # getinfo_copy.remove(i)
            continue
        elif is_pic and len(i['mblog']['pics']) == 0:
            logger.info(f' 只有一张图片，跳过')
            # getinfo_copy.remove(i)
            continue
        elif is_video and is_live:
            logger.info(f' 是直播，跳过')
            # getinfo_copy.remove(i)
            continue
        else:
            source_update_message[i['mblog']['user']['screen_name']].append(i)
    for k, v in source_update_message.items():
        source_update_message[k] = v[-WEIBO_UPDATE_NUM:]
    return source_update_message


'''
    微博的主题和源的关系
'''


def get_update_theme_and_source():
    with mysel_conf.pool().cursor() as cursor:
        # 执行sql语句，进行查询
        sql = 'SELECT theme_id,source_name  FROM theme_source_update where source_id=1 AND  theme_id in (SELECT theme_id FROM theme_auto_update where status=1)'
        cursor.execute(sql)
        # 获取查询结果
        result = cursor.fetchall()
        res = {}
        for r in result:
            res[r['source_name']] = r['theme_id']
        return res


'''
    将主题源更新的内容写入mysql
'''


def weibo_cos_insert_mysql(dict_msg):
    time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    conn = mysel_conf.pool()
    cursor = conn.cursor()
    try:
        sql = "INSERT INTO contents (theme_id,thirdparty_id, publish_time,title,desp,contents_refter_type,media_type,pic_link,likes_count,created_at,updated_at,dynamic_time,mongo_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s, %s)"
        contents = (
            dict_msg['contents']['theme_id'],
            dict_msg['contents']['thirdparty_id'],
            dict_msg['contents']['publish_time'],
            re_html_tag(dict_msg['contents']['title']),
            '',
            dict_msg['contents']['contentsType'],
            dict_msg['contents']['media_type'],
            dict_msg['contents']['pic_link'],
            random.randint(5, 50),
            time_str,
            time_str,
            time_str,
            dict_msg['contents']['mongo_id']
        )
        cursor.execute(sql, contents)  # 写contents
        id = int(cursor.lastrowid)
        update_used.insert({'scheme': dict_msg['contents']['pic_link'], 'mysql_id': id})
        # 写入图片
        sql = "INSERT INTO contents_ext(contents_sort,created_at,updated_at,contents_id,pic_link,width,height,video_link,video_ticket,bigpic_link) value(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        tmp = 0
        if dict_msg['contents']['media_type'] == 1 and len(dict_msg['ext']['pics']):
            pics_len = len(dict_msg['ext']['pics'])
            for pic in dict_msg['ext']['pics']:
                if pics_len == 1 and tmp == 0:
                    url = pic['url']
                    file = requests.get(url)
                    tmpIm = io.BytesIO(file.content)
                    im = Image.open(tmpIm)
                    w = im.size[0]
                    h = im.size[1]
                else:
                    w = 0
                    h = 0
                tmp += 1
                contents = (tmp, time_str, time_str, id, pic['url'], w, h, '', '', pic['big_url'])
                cursor.execute(sql, contents)  # 写contents
        else:
            contents = (
                tmp, time_str, time_str, id, dict_msg['ext']['video']['pic'], '0', '0',
                '',
                dict_msg['ext']['video']['stream'], dict_msg['ext']['video']['big_pic'])
            cursor.execute(sql, contents)  # 写contents
        # # 查tag 并写入
        # get_tag_from_theme_id = "SELECT tag FROM theme_update_tag WHERE theme_id = %s"
        # cursor.execute(get_tag_from_theme_id, dict_msg['contents']['theme_id'], )  # 读tag
        # result = cursor.fetchone()
        # try:
        #     tags = json.loads(result['tag'])
        #     if len(tags):
        #         sql = "insert contents_tag (content_id,tag_id,name,created_at,updated_at) values(%s,%s,%s,%s,%s)"
        #         for tag in tags:
        #             cursor.execute(sql, (id, tag, '', time_str, time_str))  # 写contents
        # except TypeError:
        #     print('无tag')
        conn.commit()  # 事务提交
    except Exception as e:
        conn.rollback()  # 事务回滚
        print('事务处理失败', e)
    else:
        conn.commit()  # 事务提交
        print('事务处理成功', cursor.rowcount)
    cursor.close()
    conn.close()


'''
    整理接口数据->mysql数据结构
'''


def format_to_mysql(dict_msg, theme_id):
    to_mysql = {}
    to_mysql['contents'] = {}
    to_mysql['ext'] = {}
    to_mysql['contents'] = {}
    # 是转发
    if dict_msg['mblog'].get('retweeted_status'):
        if dict_msg['mblog']['retweeted_status'].get('pics') and dict_msg['mblog']['retweeted_status']['pics']:
            media_type = 1

        elif dict_msg['mblog']['retweeted_status'].get('page_info') and dict_msg['mblog']['retweeted_status'][
            'page_info'] and dict_msg['mblog']['retweeted_status']['page_info'].get('media_info') and \
                dict_msg['mblog']['retweeted_status']['page_info']['media_info']:
            media_type = 2
        else:
            media_type = 4
        title = re_html_tag(dict_msg["mblog"]['text']) + '\n' + re_html_tag(
            dict_msg["mblog"]['retweeted_status']['text'])

        try:
            if re_all_text.search(dict_msg["mblog"]['text']).group(1):
                text = requests.get(
                    f"https://m.weibo.cn/statuses/show?id={ re_all_text.search(dict_msg['mblog']['text']).group(1) }").json()[
                    'data']['text']
                title = re_html_tag(text) + '\n' + re_html_tag(dict_msg["mblog"]['retweeted_status']['text'])
        except:
            pass
        if re_all_text.search(dict_msg["mblog"]['text']):
            text = requests.get(
                f"https://m.weibo.cn/statuses/show?id={ re_all_text.search(dict_msg['mblog']['text']).group(1) }").json()[
                'data']['text']
            title = re_html_tag(text) + '\n' + re_html_tag(dict_msg["mblog"]['retweeted_status']['text'])

        to_mysql['contents']['theme_id'] = theme_id
        to_mysql['contents']['publish_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        to_mysql['contents']['thirdparty_id'] = f'https://m.weibo.cn/status/{dict_msg["mblog"]["bid"] }'
        to_mysql['contents']['contentsType'] = 2
        to_mysql['contents']['title'] = title
        to_mysql['contents']['media_type'] = media_type
        to_mysql['contents']['pic_link'] = f'https://m.weibo.cn/status/{ dict_msg["mblog"]["bid"] }'
        to_mysql['contents']['mongo_id'] = f'update_py_weibo_cos:bid:{dict_msg["mblog"]["bid"]}'

        if media_type == 1:
            to_mysql['ext']['pics'] = []
            for pic in dict_msg["mblog"]['retweeted_status']['pics']:
                to_mysql['ext']['pics'].append({'big_url': pic['large']['url'], 'url': pic['url']})
        else:
            to_mysql['ext']['video']['pic'] = dict_msg['mblog']['retweeted_status']['page_info']['page_pic']['url']
            to_mysql['ext']['video']['big_pic'] = dict_msg['mblog']['retweeted_status']['page_info']['page_pic']['url']

            to_mysql['ext']['video'][
                'stream'] = f'https://m.weibo.cn/status/{dict_msg["mblog"]["retweeted_status"]["id"]}'
    else:

        if dict_msg['mblog'].get('pics') and dict_msg['mblog']['pics']:
            media_type = 1
        elif dict_msg['mblog'].get('page_info') and dict_msg['mblog']['page_info'] and dict_msg['mblog'][
            'page_info'].get('media_info') and dict_msg['mblog']['page_info']['media_info']:
            media_type = 2
        else:
            media_type = 4
        title = re_html_tag(dict_msg["mblog"]['text'])
        try:
            if re_all_text.search(dict_msg["mblog"]['text']).group(1):
                text = requests.get(
                    f"https://m.weibo.cn/statuses/show?id={ re_all_text.search(dict_msg['mblog']['text']).group(1) }").json()[
                    'data']['text']
                title = re_html_tag(text)
        except:
            pass
        to_mysql['contents']['theme_id'] = theme_id
        to_mysql['contents']['publish_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        to_mysql['contents']['thirdparty_id'] = f'https://m.weibo.cn/status/{dict_msg["mblog"]["bid"] }'
        to_mysql['contents']['contentsType'] = 2
        to_mysql['contents']['title'] = title
        to_mysql['contents']['media_type'] = media_type
        to_mysql['contents']['pic_link'] = f'https://m.weibo.cn/status/{ dict_msg["mblog"]["bid"] }'
        to_mysql['contents']['mongo_id'] = f'update_py_weibo_cos:bid:{dict_msg["mblog"]["bid"]}'
        if media_type == 1:
            to_mysql['ext']['pics'] = []
            for pic in dict_msg["mblog"]['pics']:
                to_mysql['ext']['pics'].append({'big_url': pic['large']['url'], 'url': pic['url']})
        else:
            to_mysql['ext']['video'] = {}
            to_mysql['ext']['video']['pic'] = dict_msg["mblog"]['page_info']['page_pic']
            to_mysql['ext']['video']['big_pic'] = dict_msg["mblog"]['page_info']['page_pic']
            to_mysql['ext']['video']['stream'] = f'https://m.weibo.cn/status/{dict_msg["mblog"]["id"]}'
    return to_mysql


'''
    执行方法
'''


def main():
    theme_source = get_update_theme_and_source()
    theme_msg = get_update_config_num_for_source()
    for source_name, weibo in theme_msg.items():
        theme_id = theme_source[source_name]
        for w in weibo:
            if update_used.find({'scheme': f'https://m.weibo.cn/status/{ w["mblog"]["bid"] }'}).count():
                logger.info('已存在')
                continue
            else:
                logger.info(format_to_mysql(w, theme_id))
                weibo_cos_insert_mysql(format_to_mysql(w, theme_id))


if __name__ == '__main__':
    main()
